/*# Capture ESXi system information.
#
# Written by: Alaa Hleihel <alaa@mellanox.com>
*/


// free memory, and organize the includes
#include "vars.h"
#include "methods.h"
#include "CommandList.h"

#include <sys/wait.h>
#include <iostream>
#include <sys/types.h>
#include <fcntl.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fstream>
#include <vector>
#include <cstring>
#include <sys/types.h> 

using namespace std;

//----------------------------------------------------------------------------------------
// main
int
main(int argc, char **argv)
{
	// 
	puts("Mellanox Technologies");
	puts("ESXi System Snapshot");
	printf("Version: "); puts(VERSION_STR);
	puts("\nGathering the information may take a few minutes. Please wait...\n");

	// define file name
	char *hostName[] = {"hostname", NULL};
	char *date[] = {"date", NULL};
	string htmlFilePath = "sysinfo_snapshot_" \
		+ strReplace(strReplace(strReplace(runCommand(hostName), ' ', '_'), ':', '-'), '\n', '-') \
		+ strReplace(strReplace(strReplace(runCommand(date), ' ', '_'), ':', '-'), '\n', '-') \
		+ ".html";
	
	// create html file
	ofstream htmlFile;
	htmlFile.open(htmlFilePath.c_str());
	if (!htmlFile.is_open())
	{
		cout << "ERROR: Unable to create html file\n";
		exit(1);
	}
	
	// add header, ...
	initHtml(htmlFile);
	
	// **************************
	// prepare Server Commands
	CommandList serverCommands;
	serverCommands.add("hostname");
	serverCommands.add("vmware -v");
	serverCommands.add("vmware -l");	
	serverCommands.add("esxcli vm process list");
	serverCommands.add("esxcli software vib list | grep Mellanox");
	serverCommands.add("esxcli software vib list");
	serverCommands.add("esxcli hardware pci list | grep -A 28 Mellanox");
	serverCommands.add("esxcli hardware pci list");
	//serverCommands.add("vmkload_mod -l | egrep 'mlx|ib_'");
	serverCommands.add("vmkload_mod -l | grep mlx");
	serverCommands.add("vmkload_mod -l | grep ib_");
	serverCommands.add("vmkload_mod -l");
	//serverCommands.add("esxcli system module list | egrep 'mlx|ib_'");
	serverCommands.add("esxcli system module list | grep mlx");
	serverCommands.add("esxcli system module list | grep ib_");
	serverCommands.add("esxcli system module list");
	serverCommands.add("esxcli hardware cpu global get");
	serverCommands.add("esxcli hardware platform get");
	serverCommands.add("esxcli system module parameters list -m mlx4_core");
	serverCommands.add("esxcli system module get -m mlx4_ib");
	/*serverCommands.add("");
	serverCommands.add("");
	serverCommands.add("");
	serverCommands.add("");*/
	
	
	// **************************
	// prepare Fabric Commands
	CommandList fabricCommands;	
	fabricCommands.add("esxcli network nic list | grep -i Mellanox");
	fabricCommands.add("esxcfg-nics -l");
	fabricCommands.add("esxcli network nic list");
	fabricCommands.add("esxcli network ip interface list");
	fabricCommands.add("esxcli network ip interface ipv4 get");
	fabricCommands.add("esxcli network ip connection list");
	fabricCommands.add("esxcli network ip neighbor list");
	fabricCommands.add("esxcfg-vswitch -l");
	fabricCommands.add("esxcli network vswitch standard list");
	fabricCommands.add("esxcli network vswitch standard policy failover get -v vSwitch0");
	fabricCommands.add("esxcli network vswitch standard policy security get -v vSwitch0");
	fabricCommands.add("esxcli network vswitch standard policy shaping get -v vSwitch0");
	fabricCommands.add("esxcli network vswitch standard portgroup list");
	
	
	// **************************
	// prepare file list
	CommandList fileList;
	fileList.add("/var/log/vmkernel.log");
	fileList.add("/var/log/vpxa.log");
	//fileList.push_back("");
	
	// **************************
	// add the index table
	addIndexTable(htmlFile, "Server Commands", serverCommands);
	addIndexTable(htmlFile, "Fabric Commands", fabricCommands);
	addIndexTable(htmlFile, "Files", fileList);
	// **************************
	htmlFile << "<hr>";
	
	// **************************
	// run the commands
	addCommandList(htmlFile, serverCommands);
	addCommandList(htmlFile, fabricCommands);
	addFileList(htmlFile, fileList);
	
	// finalize html file
	finalizeHtml(htmlFile);
	
	// compress the output file?
	
	// close handle
	htmlFile.close();
	//
	printf("\n\n%s file was generated.\n", htmlFilePath.c_str());
	//
    return 0;
}
// #### end of main
//----------------------------------------------------------------------------------------
